package com.smart.util;

import java.util.List;
import java.util.ArrayList;
import org.springframework.data.domain.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;

/**
 * @author lgb
 * @date 2020年6月11日 下午3:45:32
 * Copyright (C) 深圳工勘科技
 * @说明: @param <T> 分页数据封装类
 **/
public class CommonPage<T> {
	
	private Integer pageNum;
	private Integer pageSize;
	private Integer totalPage;
	private Long total;
	private List<T> list;

	/**
	 * 将SpringData分页后的list转为分页信息
	 */
	public static <T> CommonPage<T> restPage(Page<T> pageInfo) {
		CommonPage<T> result = new CommonPage<T>();
		result.setTotalPage(pageInfo.getTotalPages());
		result.setPageNum(pageInfo.getNumber());
		result.setPageSize(pageInfo.getSize());
		result.setTotal(pageInfo.getTotalElements());
		result.setList(pageInfo.getContent());
		return result;
	}

	public static <T> CommonPage<T> restPage(IPage<T> pageInfo) {
		CommonPage<T> result = new CommonPage<T>();
		result.setTotalPage((int) pageInfo.getPages());
		result.setPageNum((int) pageInfo.getCurrent());
		result.setPageSize((int) pageInfo.getSize());
		result.setTotal(pageInfo.getTotal());
		result.setList(pageInfo.getRecords());
		return result;
	}

	/**
	 * List 转 分页
	 *
	 * @param pageNum
	 * @param pageSize
	 * @param onlineUsers
	 * @param <T>
	 * @return
	 */
	public static <T> CommonPage<T> listToPage(Integer pageNum, Integer pageSize, List<T> onlineUsers) {
		CommonPage<T> page = new CommonPage<>();
		int totalCount = onlineUsers.size();
		page.setList(toPage(pageNum, pageSize, onlineUsers));

		page.setTotal((long) totalCount);
		page.setPageNum(pageNum);
		page.setPageSize(pageSize);

		long totalPage = totalCount % pageSize == 0 ? (totalCount / pageSize) : (totalCount / pageSize + 1);
		page.setTotalPage((int) totalPage);
		return page;
	}

	/**
	 * List 分页
	 */
	public static <T> List<T> toPage(int page, int size, List<T> list) {
		page = page - 1;
		int fromIndex = page * size;
		int toIndex = page * size + size;
		if (fromIndex > list.size()) {
			return new ArrayList<T>();
		} else if (toIndex >= list.size()) {
			return list.subList(fromIndex, list.size());
		} else {
			return list.subList(fromIndex, toIndex);
		}
	}

	public Integer getPageNum() {
		return pageNum;
	}

	public void setPageNum(Integer pageNum) {
		this.pageNum = pageNum;
	}

	public Integer getPageSize() {
		return pageSize;
	}

	public void setPageSize(Integer pageSize) {
		this.pageSize = pageSize;
	}

	public Integer getTotalPage() {
		return totalPage;
	}

	public void setTotalPage(Integer totalPage) {
		this.totalPage = totalPage;
	}

	public List<T> getList() {
		return list;
	}

	public void setList(List<T> list) {
		this.list = list;
	}

	public Long getTotal() {
		return total;
	}

	public void setTotal(Long total) {
		this.total = total;
	}
}